package com.renchunlin.topmenubarview

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.renchunlin.topmenubarview.utils.SizeUtils

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/13.
 * PS: Not easy to write code, please indicate.
 */
class TopMenuBarView : RelativeLayout {

    companion object {
        const val TAG = "CHEN"
    }

    //控件默认高度
    private var topMenuHeight = 0f
    private var topMenuBackground=0

    /**
     * left_按钮类型
     * <enum name="image" value="0"/>
     * <enum name="text" value="1"/>
     */
    private var topMenuLeftType = 0

    //left文字或Image左边距
    private var topMenuMarginLeft = 0f

    //left设置图片
    private var topMenuLeftImage = 0

    /*Center*/
    private var topMenuCenterTextSize = 0f
    private var topMenuCenterTextStyle = 0

    /*Right*/
    private var topMenuRightType = 0
    private var topMenuMarginRight = 0f
    private var topMenuRightImage = 0

    /*控件*/
    lateinit var layout: RelativeLayout
    lateinit var leftText: TextView
    lateinit var leftImage: ImageView
    lateinit var llLeft: LinearLayout
    lateinit var tvCenter: TextView

    /*Right*/
    lateinit var ivRight: ImageView
    lateinit var tvRight: TextView
    lateinit var llRight: LinearLayout

    constructor(context: Context?) : this(context, null)

    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
        initAttrs(attrs)
    }

    private fun initView(context: Context?) {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_top_menu, this, false)
        layout = view.findViewById(R.id.rl_layout)
        leftText = view.findViewById(R.id.tvLeft)
        leftImage = view.findViewById(R.id.ivLeft)
        llLeft = view.findViewById(R.id.llLeft)
        tvCenter = view.findViewById(R.id.tvCenter)
        /*Right*/
        ivRight = view.findViewById(R.id.ivRight)
        tvRight = view.findViewById(R.id.tvRight)
        llRight = view.findViewById(R.id.llRight)
        this.addView(view)
    }

    private fun initAttrs(attrs: AttributeSet?) {
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.TopMenuBarView)
        //控件高度
        topMenuHeight = typedArray.getDimension(
            R.styleable.TopMenuBarView_topMenuHeight,
            SizeUtils.dip2px(this.context, 44f).toFloat()
        )
        topMenuBackground = typedArray.getResourceId(
            R.styleable.TopMenuBarView_topMenuBackground,
            R.color.default_color
        )
        /*Left*/
        topMenuLeftType = typedArray.getInt(R.styleable.TopMenuBarView_topMenuLeftType, 0)
        topMenuMarginLeft =
            typedArray.getDimension(R.styleable.TopMenuBarView_topMenuMarginLeft, 15f)
        topMenuLeftImage = typedArray.getResourceId(
            R.styleable.TopMenuBarView_topMenuLeftImage,
            R.drawable.icon_back_white
        )

        /*Center*/
        topMenuCenterTextSize =
            typedArray.getDimension(
                R.styleable.TopMenuBarView_topMenuCenterTextSize,
                SizeUtils.sp2px(this.context, 16f)
            )
        topMenuCenterTextStyle =
            typedArray.getInt(R.styleable.TopMenuBarView_topMenuCenterTextStyle, 0)

        /*Right*/
        topMenuRightType = typedArray.getInt(R.styleable.TopMenuBarView_topMenuRightType, 0)
        topMenuMarginRight =
            typedArray.getDimension(R.styleable.TopMenuBarView_topMenuMarginRight, 15f)
        topMenuRightImage = typedArray.getResourceId(
            R.styleable.TopMenuBarView_topMenuRightImage,
            R.drawable.main_icon_msg
        )

        typedArray.recycle()
        //初始化自定义属性
        setTopMenuHeight()
    }

    private fun setTopMenuHeight() {
        //设置高度
        val layoutParams = layout.layoutParams
        layoutParams.height = topMenuHeight.toInt()
        layout.layoutParams = layoutParams
        layout.setBackgroundResource(topMenuBackground)
        /*left*/
        //设置left类型 0图片 1文字
        if (topMenuLeftType == 0) {
            leftText.visibility = GONE
            leftImage.visibility = VISIBLE
        } else {
            leftText.visibility = VISIBLE
            leftImage.visibility = GONE
        }
        //left文字或Image左边距
        llLeft.setPadding(SizeUtils.dip2px(this.context, topMenuMarginLeft), 0, 0, 0)
        //left设置图片
        leftImage.setImageResource(topMenuLeftImage)
        /*Center*/
        tvCenter.textSize = SizeUtils.px2sp(this.context, topMenuCenterTextSize)
        if (topMenuCenterTextStyle == 0) {
            tvCenter.setTypeface(null, Typeface.NORMAL)
        } else {
            tvCenter.setTypeface(null, Typeface.BOLD)
        }

        /*Right*/
        if (topMenuRightType == 0) {
            ivRight.visibility = VISIBLE
            tvRight.visibility = GONE
        } else {
            ivRight.visibility = GONE
            tvRight.visibility = VISIBLE
        }
        llRight.setPadding(0, 0, SizeUtils.dip2px(this.context, topMenuMarginRight), 0)
        ivRight.setImageResource(topMenuRightImage)
    }

}